package com.cheyunkeji.er.activity.evaluate;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.evaluate.BCSMCheckItemView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class StartCheckBCSMActivity extends BaseActivity {

  private static final String TAG = StartCheckBCSMActivity.class.getSimpleName();

  @BindView(R.id.v_topbar)
  TopBar vTopbar;

  @BindView(R.id.bcsm_119)
  BCSMCheckItemView bcsm119;

  @BindView(R.id.bcsm_120)
  BCSMCheckItemView bcsm120;

  @BindView(R.id.bcsm_121)
  BCSMCheckItemView bcsm121;

  @BindView(R.id.bcsm_122)
  BCSMCheckItemView bcsm122;

  @BindView(R.id.bcsm_123)
  BCSMCheckItemView bcsm123;

  @BindView(R.id.bcsm_124)
  BCSMCheckItemView bcsm124;

  @BindView(R.id.bcsm_125)
  BCSMCheckItemView bcsm125;

  @BindView(R.id.bcsm_126)
  BCSMCheckItemView bcsm126;

  @BindView(R.id.bcsm_127)
  BCSMCheckItemView bcsm127;

  @BindView(R.id.bcsm_128)
  BCSMCheckItemView bcsm128;

  @BindView(R.id.bcsm_129)
  BCSMCheckItemView bcsm129;

  @BindView(R.id.bcsm_130)
  BCSMCheckItemView bcsm130;

  @BindView(R.id.bcsm_131)
  BCSMCheckItemView bcsm131;

  @BindView(R.id.bcsm_132)
  BCSMCheckItemView bcsm132;

  @BindView(R.id.bcsm_133)
  BCSMCheckItemView bcsm133;

  @BindView(R.id.bcsm_134)
  BCSMCheckItemView bcsm134;

  @BindView(R.id.bcsm_135)
  BCSMCheckItemView bcsm135;

  @BindView(R.id.bcsm_136)
  BCSMCheckItemView bcsm136;

  @BindView(R.id.bcsm_137)
  BCSMCheckItemView bcsm137;

  @BindView(R.id.bcsm_138)
  BCSMCheckItemView bcsm138;

  @BindView(R.id.bcsm_139)
  BCSMCheckItemView bcsm139;

  @BindView(R.id.bcsm_140)
  BCSMCheckItemView bcsm140;

  @BindView(R.id.bcsm_141)
  BCSMCheckItemView bcsm141;

  @BindView(R.id.bcsm_142)
  BCSMCheckItemView bcsm142;

  @BindView(R.id.activity_start_check_bcsm)
  LinearLayout activityStartCheckBcsm;

  private Map<Integer, BCSMCheckItemView> itemViewList = null;

  private List<EvaluateDetailResult2.DetectionBean.IgnitionMarkBean> ignitionMarkList = null;

  @Override
  protected void init() {

    setContentView(R.layout.er_activity_start_check_bcsm);
    ButterKnife.bind(this);

    itemViewList = new HashMap<>();
    itemViewList.put(119, bcsm119);
    itemViewList.put(120, bcsm120);
    itemViewList.put(121, bcsm121);
    itemViewList.put(122, bcsm122);
    itemViewList.put(123, bcsm123);
    itemViewList.put(124, bcsm124);
    itemViewList.put(125, bcsm125);
    itemViewList.put(126, bcsm126);
    itemViewList.put(127, bcsm127);
    itemViewList.put(128, bcsm128);
    itemViewList.put(129, bcsm129);
    itemViewList.put(130, bcsm130);
    itemViewList.put(131, bcsm131);
    itemViewList.put(132, bcsm132);
    itemViewList.put(133, bcsm133);
    itemViewList.put(134, bcsm134);
    itemViewList.put(135, bcsm135);
    itemViewList.put(136, bcsm136);
    itemViewList.put(137, bcsm137);
    itemViewList.put(138, bcsm138);
    itemViewList.put(139, bcsm139);
    itemViewList.put(140, bcsm140);
    itemViewList.put(141, bcsm141);
    itemViewList.put(142, bcsm142);
  }

  @Override
  protected void initView() {

    vTopbar.setLeftBack();
    vTopbar.setRight("保存", -1, this);
    vTopbar.setTitle("启动检查补充说明");

    if (null != MyApplication.getInstance().getEvaluateResult().getDetection()
        && null
            != MyApplication.getInstance().getEvaluateResult().getDetection().getIgnition_mark()) {

      ignitionMarkList =
          MyApplication.getInstance().getEvaluateResult().getDetection().getIgnition_mark();

      if (ignitionMarkList.size() > 0) {

        displayInfo();
      }
    }
  }

  private void displayInfo() {

    for (EvaluateDetailResult2.DetectionBean.IgnitionMarkBean bean : ignitionMarkList) {

      itemViewList.get(bean.getId()).setCheckedIndex(bean.getStatus() - 1);
      itemViewList.get(bean.getId()).setTag(bean);
    }
  }

  @Override
  protected void initData() {}

  @Override
  public void onClick(View v) {
    super.onClick(v);

    if (ignitionMarkList == null) {
      ignitionMarkList = new ArrayList<>();
    } else {
      ignitionMarkList.clear();
    }

    if (v.getId() == R.id.vRight) {

      for (int index : itemViewList.keySet()) {
        if (3 != itemViewList.get(index).getCurrentCheckedIndex()) {

          ignitionMarkList.add(
              new EvaluateDetailResult2.DetectionBean.IgnitionMarkBean(
                  index, itemViewList.get(index).getCurrentCheckedIndex() + 1));
        }
      }

      String myData =
          new StringBuilder("{\"ignition_mark\":")
              .append(new Gson().toJson(ignitionMarkList))
              .append("}")
              .toString();

      Log.e(TAG, "onClick: " + new Gson().toJson(ignitionMarkList));

      HashMap<String, String> params = new HashMap<>();
      //            params.put("authkey",
      // UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
      //            params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(),
      // ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
      params.put("aid", MyApplication.getInstance().getEvaluateResult().getAid());

      params.put("detection", myData);

      ApiClient.postForm(
          HttpConstants.INSERT_ARCHIVE_INFO,
          params,
          new RespCallback<EvaluateDetailResult2>() {

            @Override
            protected void onRequestFailed(String msg) {
              SToast.show(msg);
            }

            @Override
            protected void onSuccess(EvaluateDetailResult2 data) {
              if (null != data) {
                MyApplication.getInstance().setEvaluateResult(data);
                SToast.show("保存成功");
                MyApplication.getInstance().removeActivity(StartCheckBCSMActivity.class);
                finish();
              }
            }

            @Override
            public void onBefore(Request request, int id) {
              showDialog();
            }

            @Override
            public void onAfter(int id) {
              dismissDialog();
            }
          });
    }
  }
}
